2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_masterRxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
35 //
36 //Set the address for slave module. This is a 7-bit address sent in the
37 //following format:
38 //[A6:A5:A4:A3:A2:A1:A0:RS]
39 //
40 //A zero in the "RS" position of the first byte means that the master
41 //transmits (sends) data to the selected slave, and a one in this position
42 //means that the master receives data from the slave.
43 //
44 //*****************************************************************************
45 #define SLAVE_ADDRESS 0x48
46 
47 //*****************************************************************************
48 //
49 //Specify Expected Receive data count.
50 //
51 //*****************************************************************************
52 #define RXCOUNT 0x05
53 
54 //******************************************************************************
55 // MSP430F5xx_6xx Demo - USCI_B0 I2C Master RX multiple bytes from MSP430 Slave
56 //
57 // Description: This demo connects two MSP430's via the I2C bus. The master
58 // reads 5 bytes from the slave. This is the MASTER CODE. The data from the slave
59 // transmitter begins at 0 and increments with each transfer.
60 // The USCI_B0 RX interrupt is used to know when new data has been received.
61 // ACLK = n/a, MCLK = SMCLK = BRCLK = DCO = ~1MHz
62 //
63 // /|\ /|\
64 // MSP430F67791A 10k 10k MSP430F67791A
65 // slave | | master
66 // ----------------- | | -----------------
67 // -|XIN P2.6/UCB0SDA|<-|----+->|P2.6/UCB0SDA XIN|-
68 // | | | | | 32kHz
69 // -|XOUT | | | XOUT|-
70 // | P2.5/UCB0SCL|<-+------>|P2.5/UCB0SCL |
71 // | | | P1.0|--> LED
72 //
73 //******************************************************************************
74 uint8_t RXData;
75 void main (void)
76 {
77  WDT_A_hold(WDT_A_BASE);
78 
79  //Set the XT1 frequency to UCS
80  UCS_setExternalClockSource(32768, 0);
81 
82  //Configure Pins for I2C (UCB0SCL, UCB0SDA)
83  //Set P2.5 and P2.6 as Module Function Input
84  GPIO_setAsPeripheralModuleFunctionInputPin(
85  GPIO_PORT_P2,
86  GPIO_PIN5 + GPIO_PIN6
87  );
88 
89  //Init I2C master
90  EUSCI_B_I2C_initMasterParam param = {0};
91  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
92  param.i2cClk = UCS_getSMCLK();
93  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
94  param.byteCounterThreshold = RXCOUNT;
95  param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
96  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
97 
98  //Specify slave address
99  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
101  );
102 
103  //Set Master in receive mode
104  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
105  EUSCI_B_I2C_RECEIVE_MODE
106  );
107 
108  //Enable I2C Module to start operations
109  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
110 
111  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
112  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
113  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
114  EUSCI_B_I2C_NAK_INTERRUPT
115  );
116 
117  //Enable master Receive interrupt
118  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
119  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
120  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
121  EUSCI_B_I2C_NAK_INTERRUPT
122  );
123 
124  //Set P1.0 as an output pin.
125  GPIO_setAsOutputPin(
126  GPIO_PORT_P1,
127  GPIO_PIN0
128  );
129 
130  while (1)
131  {
132  __delay_cycles(2000);
133 
134  while (EUSCI_B_I2C_SENDING_STOP ==
135  EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
136 
137  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
138 
139  // Enter LPM0 w/ interrupt
140  __bis_SR_register(CPUOFF+GIE);
141  }
142 }
143 
144 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
145 #pragma vector=USCI_B0_VECTOR
146 __interrupt
147 #elif defined(__GNUC__)
148 __attribute__((interrupt(USCI_B0_VECTOR)))
149 #endif
150 void USCIB0_ISR(void)
151 {
152  static uint8_t count = 0;
153  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
154  {
155  case USCI_NONE: // No interrupts break;
156  break;
157  case USCI_I2C_UCALIFG: // Arbitration lost
158  break;
159  case USCI_I2C_UCNACKIFG: // NAK received (master only)
160  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
161  break;
162  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
163  break;
164  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
165  break;
166  case USCI_I2C_UCRXIFG3: // RXIFG3
167  break;
168  case USCI_I2C_UCTXIFG3: // TXIFG3
169  break;
170  case USCI_I2C_UCRXIFG2: // RXIFG2
171  break;
172  case USCI_I2C_UCTXIFG2: // TXIFG2
173  break;
174  case USCI_I2C_UCRXIFG1: // RXIFG1
175  break;
176  case USCI_I2C_UCTXIFG1: // TXIFG1
177  break;
178  case USCI_I2C_UCRXIFG0: // RXIFG0
179  // Get RX data
180  RXData = EUSCI_B_I2C_masterReceiveSingle(
181  EUSCI_B0_BASE
182  );
183  if (++count >= RXCOUNT) {
184  count = 0;
185  __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
186  }
187  break; // Vector 24: RXIFG0 break;
188  case USCI_I2C_UCTXIFG0: // TXIFG0
189  break;
190  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
191  GPIO_toggleOutputOnPin(
192  GPIO_PORT_P1,
193  GPIO_PIN0
194  );
195  break;
196  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
197  break;
198  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
199  break;
200  default:
201  break;
202  }
203 }
204 
void USCIB0_ISR(void)
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)